import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
const Home = () => import('../views/home/home.vue')
const Login = () => import('../views/login/login.vue')

const User = () => import('../views/home/user/user.vue')
const Goods = () => import('../views/home/goods/goods.vue')
const Order = () => import('../views/home/order/order.vue')
const Setting = () => import('../views/home/setting/setting.vue')
const Rights = () => import('../views/home/rights/rights.vue')
const Admin = () => import('../views/home/admin/admin.vue')

const router = createRouter({
  history: createWebHashHistory(), // hash 模式
  routes: [
    {
      path: '/',
      component: Login
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
       component: Home,
       children: [
         { path: 'admin', component:Admin},
         { path: 'user', component:User},
         { path: 'goods', component:Goods},
         { path: 'order', component:Order},
         { path: 'setting', component:Setting},
         { path: 'rights', component:Rights},
       ]
    },
  ]
})

router.beforeEach(function(to, from, next) {
  if(to.path !== '/login'){
    const token = localStorage.getItem('token')
    if(token) {
      next()
    } else {
      ElMessage({
        message: '请登录账号！',
        type: 'warning',
      })
      next('/login')
    }

  }else {
    next()
  }
})

export default router



// import Vue from 'vue'
// import { createRouter } from 'vue-router'

// const Home = () => import('../views/home/home.vue')
// const Login = () => import('../views/login/login.vue')

// Vue.use(createRouter)

// const routes = [
//     {
//         path: '/',
//         redirect: '/login'
//     },
//     {
//         path: '/login',
//         component: Login
//     },
//     {
//         path: '/home',
//         component: Home
//     },
// ]

// const router = createRouter({
//     // base: '/supermall',
//     routes,
//     mode: 'history'
// })

// export default router